﻿/*========================== 
 * @author 郑金泉
 * @desc Memcached客户端配置信息类
 * ========================= */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using Obsidian.Utils;

namespace Obsidian.Config
{
    public class MemcachedConfig
    {

        private static DateTime modified = DateTime.MinValue;
        private static Hashtable htClient = Hashtable.Synchronized(new Hashtable());
        private static List<ClientInfo> clientList = new List<ClientInfo>();
        private static string defaultName = null;

        public static string DefaultName
        {
            get { return defaultName; }
        }

        public static ClientInfo GetClientInfo(string name)
        {
            CheckForChanged();
            if (!htClient.ContainsKey(name))
                return null;
            return (ClientInfo)htClient[name];
        }

        public static ICollection<ClientInfo> GetClientList()
        {
            CheckForChanged();
            return clientList;
        }

        public static bool CheckForChanged()
        {
            if (AppConfig.CheckForChanged() || !AppConfig.Modified.Equals(modified))
            {
                GetClientInfos();
                modified = AppConfig.Modified;
                return true;
            }
            return false;
        }

        private static void GetClientInfos()
        {
            lock (htClient)
            {
                defaultName = null;
                htClient.Clear();
                clientList.Clear();
                XmlDocument xmlDoc = AppConfig.GetXml();

                XmlNode node = xmlDoc.SelectSingleNode("appConfig/memcached");
                if (node == null)
                    return;
                if (!XmlUtil.TryGetAttrValue(node, "default", out defaultName))
                    defaultName = null;

                XmlNodeList nodeList = xmlDoc.SelectNodes("appConfig/memcached/client");
                for (int i = 0; i < nodeList.Count; i++)
                {
                    ClientInfo info = new ClientInfo();
                    node = nodeList.Item(i);
                    string val;
                    if (!XmlUtil.TryGetAttrValue(node, "name", out val))
                        throw new Exception("memcached配置项 name属性不能为空!");
                    info.SetName(val);

                    if (!XmlUtil.TryGetAttrValue(node, "server", out val))
                        throw new Exception("memcached配置项 server属性不能为空!");
                    info.SetServer(val);

                    htClient.Add(info.Name, info);
                    clientList.Add(info);
                }
            }
        }


        public class ClientInfo
        {

            private string _name;
            private string _server;

            public string Name 
            { 
                get { return _name; } 
            }

            public string Server 
            {
                get { return _server; }
            }

            internal void SetName(string name)
            {
                this._name = name;
            }

            internal void SetServer(string server)
            {
                this._server = server;
            }

        }

    }
}
